<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>


    <a-tabs v-model="activeKey" @change="handleChangeTab">
      <a-tab-pane key="tempSave" tab="暂存">
        <a-table
          ref="tempSavetable"
          size="middle"
          :scroll="{x:true}"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="tempSaveTable.dataSource"
          :pagination="tempSaveTable.ipagination"
          :loading="tempSaveTable.loading"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          class="j-table-force-nowrap1"
          @change="handlePageChange">

          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text,record">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
            <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
                 style="max-width:80px;font-size: 12px;font-style: italic;"/>
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
            <a-button
              v-else
              :ghost="true"
              type="primary"
              icon="download"
              size="small"
              @click="downloadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical"></a-divider>


              <act-apply-btn
                v-show="record.status < 0"
                @success="loadData"
                :data-id="record.id"
                :serviceName="serviceName"
                :variables="record"
              ></act-apply-btn>


              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>


              <a-divider type="vertical"/>
              
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
                <a-menu slot="overlay">
                  
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>

        </a-table>
      </a-tab-pane>
      <a-tab-pane key="approving" tab="审批中">
        <!-- 暂存内容 -->
        <a-table
          ref="approvingTable"
          size="middle"
          bordered
          :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
          class="j-table-force-nowrap1"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="approvingTable.dataSource"
          :pagination="approvingTable.ipagination"
          :loading="approvingTable.loading"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          @change="handlePageChange"
        >
          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text, record">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
            <img
              v-else
              :src="getImgView(text)"
              :preview="record.id"
              height="25px"
              alt=""
              style="max-width: 80px; font-size: 12px; font-style: italic"
            />
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
            <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical"/>
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
        </a-table>
      </a-tab-pane>
      <a-tab-pane key="needEdit" tab="需修改">
        <!-- 需修改内容 -->
        <a-table
          ref="needEditTable"
          size="middle"
          bordered
          :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
          class="j-table-force-nowrap1"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="needEditTable.dataSource"
          :pagination="needEditTable.ipagination"
          :loading="needEditTable.loading"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          @change="handlePageChange"
        >
          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text, record">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
            <img
              v-else
              :src="getImgView(text)"
              :preview="record.id"
              height="25px"
              alt=""
              style="max-width: 80px; font-size: 12px; font-style: italic"
            />
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
            <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
              <a v-if="record.status < 0" @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical"></a-divider>
              <act-apply-btn
                v-show="record.status < 0"
                @success="loadData"
                :data-id="record.id"
                :serviceName="serviceName"
                :variables="record"
              ></act-apply-btn>
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical"/>
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
        </a-table>

      </a-tab-pane>
      <a-tab-pane key="approved" tab="审批完成">
        <!-- 审批完成内容 -->
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="2">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                </span>
            </a-col>
          </a-row>
        </a-form>

        <a-table
          ref="approvedTable"
          size="middle"
          bordered
          :rowKey="
              (record, index) => {
                return record.detailId
              }
            "
          class="j-table-force-nowrap1"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="approvedTable.dataSource"
          :pagination="approvedTable.ipagination"
          :loading="approvedTable.loading"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          @change="handlePageChange"
        >
          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text, record">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
            <img
              v-else
              :src="getImgView(text)"
              :preview="record.id"
              height="25px"
              alt=""
              style="max-width: 80px; font-size: 12px; font-style: italic"
            />
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
            <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
              下载
            </a-button>
          </template>
          <span slot="action" slot-scope="text, record">
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical"/>
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
        </a-table>
      </a-tab-pane>
    </a-tabs>


    <tsl-hoilday-apply-modal ref="modalForm" @ok="modalFormOk"></tsl-hoilday-apply-modal>
  </a-card>
</template>
<script>

import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import TslHoildayApplyModal from './modules/TslHoildayApplyModal'
import ActApplyBtn from '@views/flowable/components/ActApplyBtn.vue'
import ActHistoricDetailBtn from '@views/flowable/components/ActHistoricDetailBtn.vue'
import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
import {getAction, httpAction} from '@api/manage'
import {deepClone, getDate} from '@/utils'

export default {
  name: 'TslHoildayApplyList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    ActHistoricDetailBtn, ActApplyBtn,
    TslHoildayApplyModal
  },
  data() {
    return {
      description: '请假表管理页面',
      selectedCode: '',
      subActiveKey: 'detail',
      serviceName: 'HoildayApply',
      activeKey: 'approved',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '创建人',
          align: "center",
          dataIndex: 'createBy'
        },
        {
          title: '创建日期',
          align: "center",
          dataIndex: 'createTime'
        },
        {
          title: '更新日期',
          align: "center",
          dataIndex: 'updateTime'
        },
        {
          title: '所属部门',
          align: "center",
          dataIndex: 'sysOrgCode'
        },
        {
          title: '请假开始日期',
          align: "center",
          dataIndex: 'dataBegin'
        },
        {
          title: '请假结束日期',
          align: "center",
          dataIndex: 'dataEnd'
        },
        {
          title: '请假天数',
          align: "center",
          dataIndex: 'num'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      approvedTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '3000', '10000'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      approvingTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '3000', '10000'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      needEditTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '3000', '10000'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      tempSaveTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '3000', '10000'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      url: {
        list: "/hoilday/tslHoildayApply/list",
        delete: "/hoilday/tslHoildayApply/delete",
        deleteBatch: "/hoilday/tslHoildayApply/deleteBatch",
        exportXlsUrl: "/hoilday/tslHoildayApply/exportXls",
        importExcelUrl: "hoilday/tslHoildayApply/importExcel",

      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.queryParam.createTime_begin = getDate('day', -7)
    this.queryParam.createTime_end = getDate('day', 0)
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    handleSubmit(record) {
      record.status = 999;
      httpAction(this.url.editById, record, 'PUT').then(res => {
        if (res.success) {
          this.$message.success("编辑成功！");
          this.activeKey = 'approved';
          this.loadData();
        }
      })
    },

    onSelectChangeNeedEdit(selectedRowKeys, selectedRows) {
      this.needEditTable.selectedRowKeys = selectedRowKeys
    },
    onSelectChangeApproving(selectedRowKeys, selectedRows) {
      this.approvingTable.selectedRowKeys = selectedRowKeys
    },
    onSelectChangeApproved(selectedRowKeys, selectedRows) {
      this.approvedTable.selectedRowKeys = selectedRowKeys
    },
    onSelectChangeTempSave(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.tempSaveTable.selectionRows = selectedRows;
      console.log("----------------------",this.selectedRowKeys ,this.tempSaveTable.selectionRows )
    },
    handleChangeTab() {
      switch (this.activeKey) {
        case 'tempSave':
          this.loadTempSaveData()
          break
        case 'needEdit':
          this.loadNeedEditData()
          break
        case 'approving':
          this.loadApprovingData()
          break
        case 'approved':
          this.loadApprovedData()
          console.log('approved')
          break
        default:
          break
      }
    },
    handlePageChange(pagination){

      switch (this.activeKey) {
        case 'tempSave':
          this.tempSaveTable.ipagination = pagination
          break
        case 'needEdit':
          this.needEditTable.ipagination = pagination
          break
        case 'approving':
          console.log("111111111111111",pagination)
          this.approvingTable.ipagination = pagination
          break
        case 'approved':
          this.approvedTable.ipagination = pagination
          console.log('approved')
          break
        default:
          break
      }

      console.log("+++++++++++++++",this.tempSaveTable.pagination)

      this.handleChangeTab()
    },
    loadTempSaveData() {
      console.log("-------------------------------------------")
      this.tempSaveTable.loading = true
      let param = {
        status: -2,
        createTime_begin: this.createTime_begin,
        createTime_end: this.createTime_end,
        pageNo: this.tempSaveTable.ipagination.current,
        pageSize: this.tempSaveTable.ipagination.pageSize,
      }
      getAction(this.url.list, param)
        .then((res) => {
          if (res.success) {
            this.tempSaveTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.tempSaveTable.ipagination.total = res.result.total
            } else {
              this.tempSaveTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.tempSaveTable.loading = false
        })
    },
    loadNeedEditData() {
      this.needEditTable.loading = true
      let param = {
        status: -1,
        createTime_begin: this.createTime_begin,
        createTime_end: this.createTime_end,
        pageNo: this.needEditTable.ipagination.current,
        pageSize: this.needEditTable.ipagination.pageSize,
      }
      getAction(this.url.list, param)
        .then((res) => {
          if (res.success) {
            this.needEditTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.needEditTable.ipagination.total = res.result.total
            } else {
              this.needEditTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.needEditTable.loading = false
        })
    },
    loadApprovingData() {
      this.approvingTable.loading = true
      let param = {
        status: 0,
        createTime_begin: this.createTime_begin,
        createTime_end: this.createTime_end,
        pageNo: this.approvingTable.ipagination.current,
        pageSize: this.approvingTable.ipagination.pageSize,
      }
      getAction(this.url.list, param)
        .then((res) => {
          if (res.success) {
            this.approvingTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.approvingTable.ipagination.total = res.result.total
            } else {
              this.approvingTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.approvingTable.loading = false
        })
    },
    loadApprovedData() {
      this.approvedTable.loading = true
      let param = {
        status: 999,
        createTime_begin: this.createTime_begin,
        createTime_end: this.createTime_end,
        pageNo: this.approvedTable.ipagination.current,
        pageSize: this.approvedTable.ipagination.pageSize,
      }
      getAction(this.url.list, param)
        .then((res) => {
          if (res.success) {
            this.approvedTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.approvedTable.ipagination.total = res.result.total
            } else {
              this.approvedTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.approvedTable.loading = false
        })
    },
    async loadData() {
      //分别根据单据状态查询结果并将其放在
      this.queryParam = this.getQueryParams()
      this.handleChangeTab()
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = [];
      fieldList.push({type: 'string', value: 'createBy', text: '创建人', dictCode: ''})
      fieldList.push({type: 'datetime', value: 'createTime', text: '创建日期'})
      fieldList.push({type: 'datetime', value: 'updateTime', text: '更新日期'})
      fieldList.push({type: 'string', value: 'sysOrgCode', text: '所属部门', dictCode: ''})
      fieldList.push({type: 'datetime', value: 'dataBegin', text: '请假开始日期'})
      fieldList.push({type: 'datetime', value: 'dataEnd', text: '请假结束日期'})
      fieldList.push({type: 'int', value: 'num', text: '请假天数', dictCode: ''})
      fieldList.push({type: 'string', value: 'status', text: '状态', dictCode: ''})
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>